package com.iplant.aps.serviceImpl.dao;

import com.iplant.aps.service.apsenum.aps.APSShiftPeriod;
import com.iplant.aps.service.po.aps.APSTaskPart;
import com.iplant.aps.service.po.aps.APSTaskPartPoint;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class APSTaskPartPointDAO extends BaseDAO {

    private static APSTaskPartPointDAO Instance = null;
    private static Logger logger = LoggerFactory.getLogger(APSTaskPartPointDAO.class);

    private APSTaskPartPointDAO() {
        super(APSTaskPartPoint.class);
    }

    public static APSTaskPartPointDAO getInstance() {
        if (Instance == null)
            Instance = new APSTaskPartPointDAO();
        return Instance;
    }


    public List<APSTaskPartPoint> GetAPSTaskPartPointList(BMSEmployee wLoginUser, APSShiftPeriod wAPSShiftPeriod,
                                                          Calendar wStartTime, Calendar wEndTime,
                                                          OutResult<Integer> wErrorCode) {

        return GetAPSTaskPartPointList(wLoginUser, wAPSShiftPeriod, null, null, wStartTime, wEndTime, wErrorCode);
    }

    public List<APSTaskPartPoint> GetAPSTaskPartPointList(BMSEmployee wLoginUser, APSShiftPeriod wAPSShiftPeriod,
                                                          List<Integer> wOrderIDList, OutResult<Integer> wErrorCode) {

        if (wOrderIDList == null || wOrderIDList.size() == 0)
            return new ArrayList<>();
        return GetAPSTaskPartPointList(wLoginUser, wAPSShiftPeriod, wOrderIDList, null, CalendarTool.getCalendar(2000
                , 0, 1), CalendarTool.getCalendar(2000, 0, 1), wErrorCode);

    }

    public List<APSTaskPartPoint> GetAPSTaskPartPointList(BMSEmployee wLoginUser, APSShiftPeriod wAPSShiftPeriod,
                                                          List<Integer> wOrderIDList, List<Integer> wTaskPartIDList,
                                                          OutResult<Integer> wErrorCode) {

        if (wOrderIDList == null || wOrderIDList.size() == 0)
            return new ArrayList<>();
        return GetAPSTaskPartPointList(wLoginUser, wAPSShiftPeriod, wOrderIDList, wTaskPartIDList,
                CalendarTool.getCalendar(2000, 0, 1), CalendarTool.getCalendar(2000, 0, 1), wErrorCode);

    }


    public List<APSTaskPartPoint> GetAPSTaskPartPointList(BMSEmployee wLoginUser, APSShiftPeriod wAPSShiftPeriod,
                                                          List<Integer> wOrderIDList, List<Integer> wTaskPartIDList,
                                                          Calendar wStartTime, Calendar wEndTime,
                                                          OutResult<Integer> wErrorCode) {

        Map<String, Object> wParams = new HashMap<>();
        if (wOrderIDList != null && wOrderIDList.size() > 0)
            wParams.put("OrderID", wOrderIDList);
        if (wTaskPartIDList != null && wTaskPartIDList.size() > 0)
            wParams.put("TaskPartID", wTaskPartIDList);

        wParams.put("StartTime", wStartTime);
        wParams.put("EndTime", wEndTime);
        wParams.put("ShiftPeriod", wAPSShiftPeriod.getValue());

        wParams.put("Active", 1);
        return this.SelectPOList(wLoginUser, wParams, APSTaskPartPoint.class, Pagination.getNewMaxSize(), wErrorCode);

    }

    public List<Integer> APS_QueryTaskPartPointIDListByTaskPartIDList(BMSEmployee wLoginUser,
                                                                               List<Integer> wTaskPartIDList,
                                                                               OutResult<Integer> wErrorCode) {
        List<Integer> wResult = new ArrayList<>();
        try {
            if(wTaskPartIDList==null||wTaskPartIDList.size()<=0){
                return wResult;
            }

            String wSQL = StringUtils.Format("SELECT ID FROM {0}.aps_task_partpoint where TaskPartID in ({1});",
                    DBSchemas.APS.getDBName(),StringUtils.Join(",",wTaskPartIDList));

            Map<String, Object> wParamMap = new HashMap<String, Object>();

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = this.queryForList(wSQL, wParamMap);

            for (Map<String, Object> wReader : wQueryResult) {
                wResult.add(StringUtils.parseInt(wReader.get("ID")));
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }
}
